package com.thingsgrid.appmanager.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thingsgrid.appmanager.entity.ApplicationInfo;
import com.thingsgrid.appmanager.entity.SystemSetting;
import com.thingsgrid.appmanager.service.IAppStoreService;
import com.thingsgrid.appmanager.service.IApplicationsService;
import com.thingsgrid.appmanager.util.GetID;
import com.thingsgrid.appmanager.util.TransferTime;
import com.thingsgrid.common.cache.CacheNames;
import com.thingsgrid.common.constant.ResponseCodeConstant;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.Map;


@ApiIgnore
@RestController
@RequestMapping("/AppStore")
@AllArgsConstructor
public class AppStoreController extends BladeController implements CacheNames {

	private IAppStoreService service;
	private IApplicationsService appservice;

	@GetMapping("/getapp")
	public R<Map> getApp(Integer currentPage, Integer pageSize, String app_name, String shlefDate, String putoffDate, Float version) {
		Page<ApplicationInfo> page = new Page<>(currentPage,pageSize);
		Map<String, Object> list = service.getApp(page,app_name, shlefDate, putoffDate, version);
		return R.data(list);
	}

	@PostMapping("/setting")
	public R systemSetting(Integer setting_type, String userId) {
		SystemSetting systemsetting = new SystemSetting();
		GetID getid = new GetID();
		systemsetting.setId(getid.getTemp());
		systemsetting.setCreateTime(LocalDateTime.now());
		systemsetting.setCurrentSetting(setting_type);
		systemsetting.setCreateUser(userId);
		if (setting_type == null || setting_type.equals("")) {
			systemsetting.setCurrentSetting(3);
		} else {
			systemsetting.setCurrentSetting(setting_type);
		}
		return R.status(service.save(systemsetting));
	}

	@GetMapping("/delete")
	public R deleteApp(@RequestParam String application_id) {
		return R.status(appservice.removeById(application_id));
	}
	@GetMapping("/shelvesapp")
	public R shelvesApp(@RequestParam String application_id) {
		UpdateWrapper<ApplicationInfo> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", application_id);
		updateWrapper.set("application_stats", 1);
		TransferTime transferTime = new TransferTime();
		updateWrapper.set("shelves_time",LocalDateTime.now());
		updateWrapper.set("put_Offshelvestime",transferTime.StrToLocalDate("9999-12-11 00:00:00"));
		return R.status(appservice.update(updateWrapper));
	}
	@GetMapping("/putoffapp")
	public R putoffApp(@RequestParam String application_id) {
		UpdateWrapper<ApplicationInfo> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", application_id);
		updateWrapper.set("application_stats", 0);
		updateWrapper.set("put_Offshelvestime",LocalDateTime.now());
		return R.status(appservice.update(updateWrapper));
	}

	/**
	 * 说明：添加应用信息
	 *
	 * @author lilongfei
	 * @since 2019/8/22 14:03
	 */
	@PostMapping("/insertApplication")
	@ApiOperation(value = "添加应用信息", notes = "添加应用信息", position = 9)
	public R insertApplication(ApplicationInfo applicationInfo, @RequestParam("file") MultipartFile file){
		Integer insertResult = service.insertApplication(applicationInfo,file);
		if(insertResult==0){
			return R.fail(ResponseCodeConstant.DATAACCESS_ERROR.getCode(),ResponseCodeConstant.DATAACCESS_ERROR.getValue());
		}else if(insertResult==2){
			return R.fail(ResponseCodeConstant.FILE_UPLOAD_ERROR.getCode(),ResponseCodeConstant.FILE_UPLOAD_ERROR.getValue());
		}
		return R.success(ResponseCodeConstant.RESPONSE_SUCCESS.getValue());
	}

}
